<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">管理员列表</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新增</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable ref="tableRef" :columns="columns" :query-params="queryData" :apiFetch="postUserPage">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">编辑</a>
                <a class="btn" v-if="!record.status" @click="changeStatus(record)">禁用</a>
                <a class="btn" v-else @click="changeStatus(record)">启用</a>
                <a class="btn" @click="remove(record.id)">删除</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="manager-list" setup>
import {
  postUserPage,
  postUserDelete,
  postUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const queryOptions = ref([
  {
    name: "账号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "username",
  },
  {
    name: "姓名",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "nickname",
  },
  // {
  //   name: "角色",
  //   type: "select",
  //   value: undefined,
  //   placeholder: "请选择",
  //   filed: "roleIds",
  //   list: [
  //     {
  //       label: "正常",
  //       value: 0,
  //     },
  //     {
  //       label: "禁用",
  //       value: 1,
  //     },
  //   ],
  // },


  {
    name: "角色",
    type: "select",
    value: undefined,
    placeholder: "请选择",
    filed: "roleIds",
    async: async (record) => {
      record.list = await storeCommonEnum.getCommonEnum("role");
    },
  },

  {
    name: "状态",
    type: "select",
    value: undefined,
    placeholder: "请选择",
    filed: "status",
    list: [
      {
        label: "正常",
        value: 0,
      },
      {
        label: "禁用",
        value: 1,
      },
    ],
  },
  {
    name: "时间",
    type: "range-picker",
    value: [],
    placeholder: "请选择",
    filed: "rangeDate",
    dateFields: ["startCreateTime", "endCreateTime"],
  },

  // {
  //   name: "状态",
  //   type: "select",
  //   value: undefined,
  //   placeholder: "请选择",
  //   filed: "status",
  //   list: [
  //     {
  //       label: "正常",
  //       value: 0,
  //     },
  //     {
  //       label: "禁用",
  //       value: 1,
  //     },
  //   ],

  // },
]);
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "100px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "管理员ID",
    dataIndex: "id",
    key: "id",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "账号",
    dataIndex: "username",
    key: "username",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "姓名",
    dataIndex: "nickname",
    key: "nickname",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "角色",
    dataIndex: "roleList",
    key: "roleList",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => { return record.roleList?.map(item => item.roleName).join(';') }
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    width: "200px",
    ellipsis: true,
  },

  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    // width: "150px",
    ellipsis: true,
    customRender: ({ record }) => (record.status == 0 ? "正常" : "停用"),
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const edit = (record) => {
  router.push({
    path: "/manager/edit",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  // const title = record.status == 0 ? "禁用" : "启用";
  const title = `${record.status ? "启用" : "禁用"}`;
  comfirmModal({
    content: `确认${title}`,
    onOk() {
      postUserUpdateStatus({
        id: record.id,
        status: record.status == 0 ? 1 : 0,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};

const remove = (id) => {
  comfirmModal({
    content: "确认删除该角色",
    onOk() {
      postUserDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};

const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    spaceName: "2121",
  }))
);
</script>

<style lang="less" scoped></style>
